#include "stdafx.h"
#include "dmAddInPrj2AddIn.h"
#include "NewRoadOperator.h"
#include "DlgNewRoad.h"
#include "HDiMineModel.h"
#include "hdimineview.h"
#include "RoadBuilder.h"

CNewRoadOperator::CNewRoadOperator(HDiMineView* view,IDMAddIn *addin)
:CAddInBaseOperator(view,addin)
{
}

CNewRoadOperator::~CNewRoadOperator()
{
}

int CNewRoadOperator::Init()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	if(!CAddInBaseOperator::Init())
		return 0;
	RoadBuilder* builder=new RoadBuilder;
	CNewRoadDialog* newRoad =new CNewRoadDialog();
	INT_PTR result= newRoad->DoModal();
	if (result!=1) return 0;
	CString filename=newRoad->m_dbfn;
	if(!_strcmpi(filename,"")) return 0;

	BEGIN_VALIDATE

	HDiMineModel* model=m_pView->GetModel();
	VALIDATE(!!model);
	if(filename.Find(".dmf")<0)
		filename+=".dmf";
	CVirtualDatabase* db=model->IsFileExist(filename);
	if(!!db) AfxMessageBox(sp_file_exists);
	VALIDATE(!db)
	db=AddNewDB(filename);
	VALIDATE(!!db)
	VALIDATE(builder->BuildLayers(db))
	VALIDATE(AddDB(db))
	model->SetActiveFile(db);
	((CAddInName*)GetIDMAddIn())->m_opStep=1;

	EXIT_VALIDATE
	if(newRoad) delete newRoad;
	if(builder) delete builder;
	RETURN_VALIDATE_RESULT
}